package com.lyt.reggie.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyt.reggie.common.R;
import com.lyt.reggie.dto.DishDto;
import com.lyt.reggie.entity.Dish;
import com.lyt.reggie.entity.DishFlavor;
import com.lyt.reggie.mapper.DishMapper;
import com.lyt.reggie.service.DishFlavorService;
import com.lyt.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Author:李雨涛
 * Date:2023-02-26-15:43
 * 描述：
 */
@Service

public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
            this.save(dishDto);
           Long dishId= dishDto.getId();
        List<DishFlavor> flavors=dishDto.getFlavors();
       flavors=flavors.stream().map((item)->{
           item.setDishId(dishId);
           return item;
       }).collect(Collectors.toList());
       dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdFlavor(Long id) {
        Dish dish = this.getById(id);
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(list);

        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        this.updateById(dishDto);
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);


    }

    @Override
    public void stops(int status, String ids) {
        if(status==1){
            if(ids.indexOf(",")==-1){

            }
        }
    }


}
